%% Figure 1
clear Q_ret_Spread_EW LS_ret_Spread_EW
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% INSERT SORTING VARIABLE AND RETURNS HERE
Sort_Var = (Stock_PRisk); 
returns = (call_deltahedge_returns_rb(1:end,:))*100;  
  % Note that data call and put option returns start from 02/2003, while 
  % PRisk start from 01/2003
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

for i = 1:size(Sort_Var,1)
    id1m = ~isnan(Sort_Var(i,:)+returns(i,:));  
    Returns = returns(i,:); Returns(:,find(id1m == 0)) = [];
    Betas_1M = Sort_Var(i,:); Betas_1M(:,find(id1m == 0)) = [];
      
    q_points_Spread_1M = prctile(Betas_1M, [0 20 40 60 80 100], 2);
    
    for j = 1: size(q_points_Spread_1M,2)-1
       Q_ret_Spread_EW(i,j) = nanmean(Returns(1,Betas_1M > q_points_Spread_1M(1,j)...
         & Betas_1M <= q_points_Spread_1M(1,j+1)));
    end   
end


% Equal-weighted returns
LS_ret_Spread_EW = Q_ret_Spread_EW(:,5)-Q_ret_Spread_EW(:,1); 

figure(1); subplot(2,1,1)
bar(LS_ret_Spread_EW); 
title('Q5-Q1 call returns')

clear Sort_Var returns Factors rf NW i j k stats_Spread_EW Q_ret_Spread_EW returns ...
      stats_Spread_FFC_EW stats_Spread_VW Factors Betas_1M q_points_Spread_1M ...
      LS_ret_Spread_EW Factors1 Factors2

% Figure 1
clear Q_ret_Spread_EW LS_ret_Spread_EW
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% INSERT SORTING VARIABLE AND RETURNS HERE
Sort_Var = (Stock_PRisk); 
returns = (put_deltahedge_returns_rb(1:end,:))*100;  
  % Note that data call and put option returns start from 02/2003, while 
  % PRisk start from 01/2003
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

for i = 1:size(Sort_Var,1)
    id1m = ~isnan(Sort_Var(i,:)+returns(i,:));  
    Returns = returns(i,:); Returns(:,find(id1m == 0)) = [];
    Betas_1M = Sort_Var(i,:); Betas_1M(:,find(id1m == 0)) = [];
      
    q_points_Spread_1M = prctile(Betas_1M, [0 20 40 60 80 100], 2);
    
    for j = 1: size(q_points_Spread_1M,2)-1
       Q_ret_Spread_EW(i,j) = nanmean(Returns(1,Betas_1M > q_points_Spread_1M(1,j)...
         & Betas_1M <= q_points_Spread_1M(1,j+1)));
    end   
end


% Equal-weighted returns
LS_ret_Spread_EW = Q_ret_Spread_EW(:,5)-Q_ret_Spread_EW(:,1); 

figure(1); subplot(2,1,2)
bar(LS_ret_Spread_EW); 
title('Q5-Q1 put returns')

clear Sort_Var returns Factors rf NW i j k stats_Spread_EW Q_ret_Spread_EW returns ...
      stats_Spread_FFC_EW stats_Spread_VW Factors Betas_1M q_points_Spread_1M ...
      LS_ret_Spread_EW Factors1 Factors2